package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dao.DishFlavorDao;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> pageSelect(Integer page,Integer pageSize,String name){
        Page<DishDto> dishDtoPage = dishService.pageSelect(page,pageSize,name);
        return R.success(dishDtoPage);
    }

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> saveDish(@RequestBody DishDto dishDto){
        dishService.saveDish(dishDto);
        return R.success("添加成功");
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> selectDishById(@PathVariable Long id){
        DishDto dishDto = dishService.getDishDto(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> updateDish(@RequestBody DishDto dishDto){
        dishService.updateDishDto(dishDto);
        return R.success("修改成功");
    }

    /**
     * (批量)删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids){
        return dishService.deleteByIds(ids);
    }

    /**
     * (批量)停售/启售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> stopSellingDish(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.updateStatus(status,ids);
        return R.success("操作成功");
    }

    /**
     * 根据分类id查询菜品集合展示在客户端
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> selectDishByCategoryId(Long categoryId,String name){
        List<DishDto> dishDtos = dishService.selectDishByCategoryId(categoryId,name);
        return R.success(dishDtos);
    }
}
